package com;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

import struct.ItemInfo;

@SuppressWarnings("serial")
public class DirectToItemDetailPageServlet extends HttpServlet 
{//client-server style (get input from client, then redirect output)
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException 
	{
		String username = req.getParameter("username");
		String itemID = req.getParameter("itemID");
		
		req.setAttribute("username", username);
		req.setAttribute("itemInfo", GetTestData(itemID));
		
		req.getRequestDispatcher("itemDetail.jsp").forward(req, resp);
	}
	
	public ItemInfo GetTestData(String itemID)
	{
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		

		Entity auction;
		ItemInfo item = new ItemInfo();
		try {
			auction = ds.get(KeyFactory.createKey("Auction", Integer.parseInt(itemID)));
			
			
			String start = "";
			String end = "";
			String current="";
			java.util.Date d1,d2,d3;
			long minutes=-1;
			long hours=-1;
			long days=-1;
			double currBid;
			
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
			
			java.util.Date currentDate = new java.util.Date();	
			current = df.format(currentDate);
			
		
			item.itemID = auction.getKey().getId();
			//System.out.println(item.itemID);

			item.description = (String) auction.getProperty("DESCRIPTION");
							
			
			item.itemName = (String) auction.getProperty("NAME");
			//System.out.println(item.itemName);
			
			
			//get highest bid 
			Query qBids = new Query("Bid").addFilter("ITEMID", FilterOperator.EQUAL, Long.toString(item.itemID) );
			Iterable<Entity> bids = ds.prepare(qBids).asIterable();
			double maxBid =Double.parseDouble(auction.getProperty("PRICE").toString());
			for(Entity bid : bids )
			{
				currBid = (Double) bid.getProperty("OFFER");
				if(currBid > maxBid){
					maxBid = currBid;
				}
			}
			item.currentBid = maxBid;
			
			
			start =  (String) auction.getProperty("START");
			end =  (String) auction.getProperty("END");

			try {
				
				
				d1 = df.parse(start);
				d2 = df.parse(end);
				d3 = df.parse(current);
				
				 if (d3.after(d2)){
						//do not display, expired
				}else if(d3.before(d1)){
					minutes = (d2.getTime()-d1.getTime())/(1000*60)%60;
					hours = (d2.getTime()-d1.getTime())/(1000*60*60)%24;
					days = (d2.getTime()-d1.getTime())/(1000*60*60*24);
					item.timeLeft = (days + "d" + hours + "h" +minutes+"m");
					
					item.img = "ta1.jpg";
					item.seller = (String) auction.getProperty("SELLER");
					

				}else{
					minutes = (d2.getTime()-d3.getTime())/(1000*60)%60;
					hours = (d2.getTime()-d3.getTime())/(1000*60*60)%24;
					days = (d2.getTime()-d3.getTime())/(1000*60*60*24);
					item.timeLeft = (days + "d" + hours + "h" +minutes+"m");
					
					item.img = "ta1.jpg";
					item.seller = (String) auction.getProperty("SELLER");
					

				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (EntityNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		

								
		return item;
	}
}
